package com.suneast.suneaststore.modules.personal.order.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.suneast.suneaststore.R;
import com.suneast.suneaststore.bean.personal.order.ListBean;

public class OrdersAdapter extends BaseAdapter<BaseViewHolder, ListBean.Order> {
    private OnOrderClickListener listener;

    public interface OnOrderClickListener {
        void onOrderClick(int orderId, int orderStatus);
    }

    public void setOnOrderClickListener(OnOrderClickListener listener) {
        this.listener = listener;
    }


    private OrderButtonClickListener buttonClickListener;

    public interface OrderButtonClickListener {
        void onButtonClick(int orderId, int pos, int orderInfo, int btnId);
    }

    public void setOrderButtonClickListener(OrderButtonClickListener listener) {
        buttonClickListener = listener;
    }


    private int listType; // 0 purchase 1 sale

    public OrdersAdapter(int listType) {
        this.listType = listType;
    }

    @Override
    public int getItemViewType(int position) {
        ListBean.Order order = list.get(position);
        switch (order.getType()) {
            case 4: // 快速订单 --> layouts[] 1
                return (order.getDiyorder_status() << 16) | 1;
            case 6: // 定制订单 --> layouts[] 2
                return (order.getDiyorder_status() << 16) | 2;
            case 0: // 普通订单 --> layouts[] 0
            default:
                return order.getStatus() << 16;
        }
    }

    private final static int layouts[] = new int[]{
            R.layout.item_order_standard,
            R.layout.item_order_fast,
            R.layout.item_order_customized
    };

    @NonNull
    @Override
    public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(layouts[viewType & 0xff], parent, false);
        view.setTag(new LinearLayoutManager(parent.getContext()));
        switch (viewType & 0xff) {
            case 1: // 已在getType中改为0普通 1快速 2定制
                return new VHFast(view, viewType >> 16, listType);
            case 2:
                return new VHCustomized(view, viewType >> 16, listType);
            case 0:
            default:
                return new VHStandard(view, viewType >> 16, listType);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull BaseViewHolder holder, int position) {
        ListBean.Order order = list.get(position);
        holder.buttons[0].setOnClickListener(v -> {
            Log.e("tag", "onClick: s0");
            if (buttonClickListener != null) {
                buttonClickListener.onButtonClick(order.getId(), position, holder.getItemViewType(),
                        R.id.btn_left);
            }
        });
        holder.buttons[1].setOnClickListener(v -> {
            Log.e("tag", "onClick: s1");
            if (buttonClickListener != null) {
                buttonClickListener.onButtonClick(order.getId(), position, holder.getItemViewType(),
                        R.id.btn_right);
            }
        });
        holder.itemView.setOnClickListener(v -> {
            if (listener != null) {
                listener.onOrderClick(order.getId(), holder.getItemViewType());
            }
        });
        holder.updateData(order);
    }
}